package com.tanjie.blackbean.auth.controller;

import com.tanjie.blackbean.pojo.auth.GrantType;
import com.tanjie.blackbean.auth.granter.GranterHolder;
import com.tanjie.blackbean.pojo.auth.AuthParam;
import com.tanjie.blackbean.pojo.auth.UsernamePassword;
import com.tanjie.blackbean.pojo.auth.AuthInfo;
import com.tanjie.blackbean.pojo.system.ClientType;
import com.tanjie.blackbean.pojo.web.BlackbeanHttpHeaders;
import com.tanjie.blackbean.web.annotation.BlackBeanController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

/**
 * @author: tanjie
 * @date: 2022-04-21
 **/
@Slf4j
@BlackBeanController
public class AuthController {

    @PostMapping("token")
    public AuthInfo tokenHandler(
            @RequestHeader(BlackbeanHttpHeaders.CLIENT_TYPE) ClientType clientType,
            @RequestBody @Validated UsernamePassword usernamePassword) {
        AuthParam authParam = AuthParam.builder()
                .usernamePassword(usernamePassword)
                .clientType(clientType)
                .grantType(GrantType.PASSWORD)
                .build();
        return GranterHolder.get(GrantType.PASSWORD).grant(authParam);
    }


}
